using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DailyTransaction
{
    public partial class frmVoidUpd : Form
    {
        private BAO bao = new BAO();
        private string fVAT_Center;
        private DataTable tblInv = null;
        DataTable tblDescription = null;
        DataTable tblReasonType = null;
        public frmVoidUpd()
        {
            InitializeComponent();
        }

        private void frmVoid_Load(object sender, EventArgs e)
        {
            uS_Disable_EditControls();
            dtpDateV.Value = DateTime.Now;

            DataTable tbl = bao.get_qVAT_Center();
            if (tbl != null)
            {
                tbl.Rows.RemoveAt(0);
                cbVATcen.DataSource = tbl;
                cbVATcen.DisplayMember = "VAT Center";
                cbVATcen.ValueMember = cbVATcen.DisplayMember;
            }

            tblReasonType = bao.get_Reason_Type();
            if (tblReasonType != null)
            {
                cbReasonType.DataSource = tblReasonType;
                cbReasonType.DisplayMember = "Reason_Type";
                cbReasonType.ValueMember = "Reason_ID";
            }
        }
        private void uS_Disable_EditControls()
        {
            cbVATcen.Enabled = true;            
            txtVATnum.Enabled = true;
            txtVATnum.ReadOnly = false;
            cbVATcen.Focus();

            txtPatName.ReadOnly = true;
            txtInvDate.ReadOnly = true;
            txtInvAmt.ReadOnly = true;
            txtCMSInv.ReadOnly = true;
            txtCMSCen.ReadOnly = true;
            rtxtRemark.ReadOnly = true;
            btnConfirm.Enabled = false;
            btnCancel.Enabled = false;
            
        }

        private void txtVATnum_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter )
            {
                if (cbVATcen.SelectedValue == null)
                    return;
                else
                    fVAT_Center = cbVATcen.SelectedValue.ToString();

                if (txtVATnum.Text == "")
                {
                    txtVATnum.Focus();
                    return;
                }

                tblInv = bao.get_Invoice(fVAT_Center,txtVATnum.Text.Trim());
                if (tblInv.Rows.Count > 1)
                {
                    MessageBox.Show("Duplicate VAT Number/ Center for this Invoice. \n Changes not Updated !!");
                    txtVATnum.Focus();
                    return;
                }
                if (tblInv == null || tblInv.Rows.Count == 0)
                {
                    MessageBox.Show("Record not found !!");
                    return;
                }
                DataRow r = tblInv.Rows[0];
                txtInvDate.Text = r["DATE_VISIT"].ToString();
                txtPatName.Text = r["PAT_NAME"].ToString() + " " + r["PAT_F_NAME"].ToString();
                txtInvAmt.Text = (float.Parse(r["TOTAL_PAY"].ToString()) + float.Parse(r["VAT_AMOUNT"].ToString()) + float.Parse(r["SRV_AMOUNT"].ToString())).ToString();
                txtCMSInv.Text = r["INV_NBER"].ToString();
                txtCMSCen.Text = r["CMS_Center"].ToString();
                rtxtRemark.Text = r["UPD_REMARK"].ToString();
                    
                uS_Enable_EditControls();    

            }
        }       
        private void uS_Enable_EditControls()
        {

            txtInvDate.ReadOnly = true;
            txtPatName.ReadOnly = true;
            txtInvAmt.ReadOnly = true;
            txtCMSInv.ReadOnly = true;
            txtCMSCen.ReadOnly = true;

            rtxtRemark.ReadOnly = false;
            btnConfirm.Enabled = true;
            btnCancel.Enabled = true;
      
            txtCMSInv.Focus();
            txtVATnum.ReadOnly = true;
            cbVATcen.Enabled = false;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbReasonType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbReasonType.SelectedValue == null)
                return;

            tblDescription = bao.get_Description(cbReasonType.SelectedValue.ToString());
            if (tblDescription != null)
            {
                cbDesc.DataSource = tblDescription;
                cbDesc.DisplayMember = "Reason_Des";
                cbDesc.ValueMember = "Reason_Des";
            }
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            uS_Disable_EditControls();
        }

        private void btnConfirm_Click(object sender, EventArgs e)
        {
            string   uVOID_Dt;
            string uCMS_INV_Nber, uOld_Payment_mode;
            bool uSUN_Extracted;

            uVOID_Dt = dtpDateV.Value.ToShortDateString();
            if (txtVATnum.Text.Trim() == "")
            {
                MessageBox.Show("Please specify VAT Nummber !!");
                txtVATnum.Focus();
                return;
            }

            if (tblInv == null || tblInv.Rows.Count == 0)
                return;

            DataRow r = tblInv.Rows[0];
            uOld_Payment_mode = r["UPD_PAY_ACCTCODE"] == null ? "" : r["UPD_PAY_ACCTCODE"].ToString();
            if (uOld_Payment_mode.Equals("") == false)
            {
                DialogResult rslt = MessageBox.Show("This record's current payment mode is : " + uOld_Payment_mode.ToUpper()
                                                    + "\nDo you want to VOID it ?", "Question", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (rslt == DialogResult.Cancel)
                {
                    return;
                }
            }

            uSUN_Extracted = Convert.ToBoolean(r["UPD_SUN_IMPORT"]);
            if (uSUN_Extracted)
            {
                DialogResult rslt = MessageBox.Show("The existing recrod already Extract to SUN Accounting interface"
                                                    + "\nDo you still want to VOID it ?", "Exclamation", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation);

                if (rslt == DialogResult.Cancel)
                    return;
            }
            string upd_acctcode = "VOID";
            string upd_inv_stat = "VOID";
            string upd_pmnt_date = dtpDateV.Value.ToShortDateString();
            string upd_remark = rtxtRemark.Text;
            string vat_num = r["upd_vat_num"].ToString();
            string upd_reason = cbDesc.SelectedValue!=null ?  cbDesc.SelectedValue.ToString() : "";
            //string upd_reason = cbDesc.SelectedValue != null ? get_description_type(cbDesc.SelectedValue.ToString()) : "";
            //string upd_reason_type = cbReasonType.SelectedValue!=null? cbReasonType.SelectedValue.ToString():"";
            string upd_reason_type = cbReasonType.SelectedValue != null ? get_reason_name(cbReasonType.SelectedValue.ToString()) : "";
            
            uCMS_INV_Nber = r["INV_NBER"].ToString();
            bao.update_Invoice_VOID(fVAT_Center, vat_num, upd_acctcode, upd_inv_stat, upd_pmnt_date, upd_remark, upd_reason, upd_reason_type);

            // Then update the Act_Proc & Act_Phar detail item entries            
            bao.update_Proc_Phar_VOID(uCMS_INV_Nber);

            MessageBox.Show("CMS Invoice VOIDed !!");
            uS_Disable_EditControls();

        }

        private string get_reason_name(string rID)
        {

            foreach (DataRow r in tblReasonType.Rows)
            {
                if (r["Reason_ID"].ToString() == rID)
                    return r["Reason_Type"].ToString();
            }
            return "";
        }

        private void txtVATnum_Leave(object sender, EventArgs e)
        {            
            txtVATnum_KeyDown(sender, new KeyEventArgs(Keys.Enter));
        }           
    }
}